package com.xw.wuliu.task;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.corey.utils.HttpDownloader;
import com.xw.wuliu.MainActivity;

import android.os.AsyncTask;

public class LoginTask extends AsyncTask<Void, Void, SoapObject>{

	private static final String NAMESPACE = "http://tempuri.org/";

	// WebService地址
	private static String URL = "http://192.168.1.3/dream805/WebService.asmx";

	private static final String METHOD_NAME = "login";
	
	private final String PRO_PSW  = "password";
	private final String PRO_NAME = "userPsname";


	private static String SOAP_ACTION = "http://tempuri.org/login";
	
	private String mName = "";
	private String mPsw  = "";
	
	private MainActivity mAct;

	public LoginTask(String aName,String aPsw,MainActivity aAct){
		mName = aName;
		mPsw  = aPsw;
		mAct  = aAct;
	}
	
	@Override
	protected void onPostExecute(SoapObject result) {
		super.onPostExecute(result);
		if ( result == null ){
			
		}else{
			//String aResult = result;
			mAct.resultHandle(true);
		}
		
	}

	@Override
	protected SoapObject doInBackground(Void... params) {
		SoapObject result = getResult();
		
		return result;
	}
	
	public SoapObject getResult() {
//		HttpDownloader hd = new HttpDownloader();
//		String resultStr = hd.download("http://192.168.1.3/dream805/WebService.asmx/login?userPsname=sa&password=1234");
//		System.out.println("result:"+resultStr);
//		return resultStr;
		
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			rpc.addProperty(PRO_PSW , mPsw);
			rpc.addProperty(PRO_NAME, mName);

			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			
			HttpTransportSE ht = new HttpTransportSE(URL);

			ht.debug = true;

			ht.call(SOAP_ACTION, envelope);

			if ( envelope.getResponse() != null ) {
				SoapObject  detail = (SoapObject) envelope.getResponse();
				return detail;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

}
